#coding=utf-8
import httplib
httplib.HTTPConnection.debuglevel=1
#增加商品详情页的提取，菜单没有增加循环提现的功能

import urllib
import urllib2
import cookielib
import BeautifulSoup
import random
import re
#import urlparse
import sqlite3


class tmallShop(object):
    '''Url must be the main url of the tmall shop.'''
    ''
    def __init__(self,url):
        f=open('detail_cat.txt','a')
        f2=open('detail_id.txt','a')
        self.curmainurl=url
        self.id=0
        self.data=[]#detail data
        self.data2=[]#detail data
        self.menudict={}#menu
        self.cat=''
        self.__getmenu()
        self.__getdetailpage()
        
        for i in self.data:
            f.write('#'.join(i))
            f.write('\r\n')
        f.close
        for j in self.data2:
            f2.write('#'.join(j))
            f2.write('\r\n')
        f2.close
        
        
    def __getmenu(self):
        cj = cookielib.CookieJar()
        opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
        response_main=opener.open(urllib2.Request(self.curmainurl))
        content=response_main.read().decode('gbk').encode('utf-8')
        soup = BeautifulSoup.BeautifulSoup(content)
        turl='http://list.tmall.com/search_product.htm'
        
        #soup.find(name, attrs, recursive, text)
        tmall_menulist=soup.find('div',{'class':'j_Cate attr'}).findAll('a',{'data-f':'spu-cat','data-c':'cat'})

        for temp_menu in tmall_menulist:
            father_menu_url=str(turl+temp_menu['href'])[:-9]
            father_menu_name=temp_menu.b.contents[0]#.decode('utf-8')
            self.menudict[father_menu_name]=father_menu_url
        print self.menudict

        
    def __getdetailpage(self):
        for self.cat,url in self.menudict.items():
            self.__getdetailpage2(url)
            
    def __getdetailpage2(self,urlnext):
        cj = cookielib.CookieJar()
        opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
        while urlnext:
            print urlnext
            response_detail=opener.open(urllib2.Request(urlnext))
            content_detail=response_detail.read().decode('gbk').encode('utf-8')
            print content_detail
            soup_detail = BeautifulSoup.BeautifulSoup(content_detail)
            self.__getdata(soup_detail)
            try:
                urlnext=soup_detail.find('a',{'class':'ui-page-next'})['href'][:-9]
                urlnext='http://list.tmall.com/search_product.htm'+urlnext
            except:
                urlnext=None

    def __getdata(self,soup): 
        for detail in soup.find('div',{'class':'view  grid-34  clearfix'}).findAll('div',{'class':'product'}):
            #print 'ok'
            detail_name=detail.find('p',{'class':'productTitle'}).a.contents[0]
            detail_id=detail['data-id']
            detail_href='http://detail.tmall.com/item.htm?id='+detail_id
            detail_price=detail.find('p',{'class':'productPrice'}).em['title']
            detail_shop=detail.find('p',{'class':'productStatus'}).find('span',{'data-icon':'small'})['data-nick']
            detail_salenum=detail.find('p',{'class':'productStatus'}).span.em.contents[0][:-1]
            self.id+=1
            #print (str(self.id),self.cat,detail_name,detail_id,detail_href,detail_price)
            self.data.append((str(self.id),self.cat,detail_shop,detail_salenum,detail_name,detail_id,detail_href,detail_price))
            
class getpagedata_fromid(object):
    def __init__(self,id):
        self.id=id
        self.cururl='http://detail.tmall.com/item.htm?id='+id
        self.xiangqingdict={}
        self.cj = cookielib.CookieJar()
        self.opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(self.cj))
        self.__getiddata1()
        
    def __getiddata1(self):
        res=self.opener.open(self.cururl)
        mydict={}
        contents=res.read().decode('gbk').encode('utf-8')
        soup=BeautifulSoup.BeautifulSoup(contents)
        soup_attrul=soup.find('ul',{'id':'J_AttrUL'})
        for i in soup_attrul.findAll('li'):
            x,y=str(i.contents[0]).split(':')
            mydict[x]=y
        mydict['id']=self.id
        #print self.xiangqingdict
        return mydict
        
            
                
if __name__=='__main__':
    '''
    caturl='http://list.tmall.com/search_product.htm?active=1&from=sn_1_cat&area_code=330100&search_condition=7&style=g&sort=s&n=60&s=0&cat=50074933'
    meitong=tmallShop(caturl)
    '''
    id='17727594868'
    t=getpagedata_fromid(id)